package server

import (
	"context"
	"gitee.com/smallow/srpc/infrastructure/business"
	"gitee.com/smallow/srpc/infrastructure/model"
	"gitee.com/smallow/srpc/infrastructure/proto/pb"
	"github.com/pkg/errors"
	"log"
)

type DepartmentService struct{}

//创建部门,支持批量创建
func (d *DepartmentService) CreateDepartments(context context.Context, reqData *pb.CreateDepartmentsReq) (*pb.CreateDepartmentsResp, error) {
	if reqData != nil && len(reqData.Department) > 0 {
		log.Println(reqData.Department)
		var departmentArr []*model.Department
		for _, v := range reqData.Department {
			depart := &model.Department{
				Name:          v.Name,
				ShortName:     v.ShortName,
				ContactPerson: v.ContactPerson,
				ContactPhone:  v.ContactPhone,
				State:         int(v.State),
				ParentID:      int(v.ParentId),
				ParentName:    v.ParentName,
				CompanyID:     int(v.CompanyId),
				BaseModel:     model.BaseModel{AppID: v.AppId},
			}
			departmentArr = append(departmentArr, depart)
		}
		err := business.SaveBatches(departmentArr)
		if err == nil {
			return &pb.CreateDepartmentsResp{
				Code:    1,
				Message: "批量创建成功",
			}, nil
		} else {
			return &pb.CreateDepartmentsResp{
				Code:    -1,
				Message: "批量创建部门出错",
			}, err
		}
	}
	return &pb.CreateDepartmentsResp{
		Code:    -1,
		Message: "缺少参数",
	}, nil
}

//创建部门账号
func (d *DepartmentService) CreateDepartmentDA(context context.Context, req *pb.CreateDepartmentDAReq) (*pb.CreateDepartmentDAResp, error) {
	departmentDA := &model.Account{
		BaseModel:     model.BaseModel{AppID: req.AppID},
		BodyID:        int(req.BodyID),
		BodyType:      3, //'账号主体类型1-公司,2-人员,3-部门',
		State:         uint8(req.State),
		BindPhone:     req.BindPhone,
		NickName:      req.NickName,
		AccountNumber: req.AccountNumber,
		Password:      business.GetMd5String(req.Password),
		WxOpenID:      req.WxOpenID,
		AliPayID:      req.AliPayID,
	}
	if business.ExistAccount(&model.Account{BodyID: departmentDA.BodyID, BodyType: departmentDA.BodyType}) {
		return &pb.CreateDepartmentDAResp{
			Code:    -1,
			Message: "该部门已创建过账号",
		}, nil
	}
	if business.ExistPhone(departmentDA.BindPhone) {
		return &pb.CreateDepartmentDAResp{
			Code:    -1,
			Message: "手机号已绑定过账号",
		}, nil
	}
	err := business.SaveDepartmentDA(departmentDA)
	if err != nil {
		return &pb.CreateDepartmentDAResp{
			Code:    -1,
			Message: "创建部门账号出错",
		}, err
	} else {
		return &pb.CreateDepartmentDAResp{
			Code:    1,
			Message: "success",
		}, nil
	}
}

//查询部门人员以及子部门信息
func (d *DepartmentService) FindDepartment(context context.Context, req *pb.FindDepartmentReq) (*pb.FindDepartmentResp, error) {
	departmentID := req.DepartmentId
	companyID := req.CompanyId
	if departmentID != 0 && companyID != 0 {
		users, childDepartments, err := business.FindDepartment(int(departmentID), int(companyID))
		if err == nil {
			childDepartmentsArr := make([]*pb.ChildrenDepartments, 0)
			usersArr := make([]*pb.Users, 0)
			for _, cp := range childDepartments {
				childDepartmentsArr = append(childDepartmentsArr, &pb.ChildrenDepartments{
					DepID:   int64(cp.Id),
					DepName: cp.Name,
				})
			}
			for _, user := range users {
				usersArr = append(usersArr, &pb.Users{
					PersonID:   int64(user.Id),
					PersonName: user.Name,
				})
			}
			return &pb.FindDepartmentResp{
				ChildrenDepartments: childDepartmentsArr,
				Users:               usersArr,
				Code:                1,
				Message:             "查询成功",
			}, nil
		} else {
			return &pb.FindDepartmentResp{
				Code:    -1,
				Message: "查询部门信息出错",
			}, err
		}
	} else {
		return &pb.FindDepartmentResp{
			Code:    -1,
			Message: "缺少参数",
		}, nil
	}
}

//修改部门信息
func (d *DepartmentService) UpdateDepartment(context context.Context, req *pb.UpdateDepartmentReq) (*pb.UpdateDepartmentResp, error) {
	if req.Id == 0 {
		return nil, errors.New("缺少参数")
	}
	department := map[string]interface{}{
		"name":           req.Name,
		"short_name":     req.ShortName,
		"contact_person": req.ContactPerson,
		"contact_phone":  req.ContactPhone,
		"state":          int(req.State),
		"parent_id":      int(req.ParentId),
		"parent_name":    req.ParentName,
	}
	err := business.UpdateDepartment(department, int(req.Id))
	if err == nil {
		return &pb.UpdateDepartmentResp{
			Code:    1,
			Message: "success",
		}, nil
	}
	return &pb.UpdateDepartmentResp{
		Code:    1,
		Message: "修改部门信息出错",
	}, err
}

//删除部门
func (d *DepartmentService) DeleteDepartment(context context.Context, req *pb.DeleteDepartmentReq) (*pb.DeleteDepartmentResp, error) {
	if req.Id == 0 || req.AppId == "" {
		return &pb.DeleteDepartmentResp{Code: -1, Message: "缺少参数"}, nil
	}
	err := business.DeleteDepartment(int(req.Id), req.AppId)
	log.Println(err)
	return nil, nil
}
